const mdpdf = require("mdpdf")
const path = require("path")
function pdf(mdPath,outputPath,cssPath,format){
    let options = {
        source:path.join(__dirname,`../markdown/${mdPath}`),
        destination:path.join(__dirname,`../pdf/${outputPath}`),
        styles:path.join(__dirname,`../css/${cssPath||"index.css"}`),
        pdf:{
            format:format||"A4",
            orientation:"portrait"
        }
    };
    mdpdf.convert(options).then((pdfPath)=>{
        console.log("PDF Output Path",pdfPath)
    }).catch((err)=>{
        console.log(err)
    })
}
let buildPdf = async (ctx,next) =>{
    let content = ctx.request.body
    pdf(content.mdPath,content.pdfPath,content.cssPath,content.format)
    ctx.response.body = {code:200,msg:"转换成功！",data:{
        mdPath:"/markdown/"+content.mdPath,
        pdfPath:"/pdf/"+content.pdfPath,
        cssPath:content.cssPath||"/css/index.css",
        format:content.format||"A4"   
    }}
}

module.exports={buildPdf}